﻿using DmxTestTool.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DmxTestTool.UI
{
    /// <summary>
    /// DMXViewPanelControl.xaml 的交互逻辑
    /// </summary>
    public partial class DMXViewPanelControl : UserControl
    {
        public static readonly DependencyProperty DmxViewPanelSourceProperty;
        public static readonly DependencyProperty DmxViewSelectIndexProperty;

        static DMXViewPanelControl()
        {
            DmxViewPanelSourceProperty = DependencyProperty.Register("DmxViewPanelSource", typeof(List<DMXViewUIModel>), typeof(DMXViewPanelControl),
                new PropertyMetadata(new List<DMXViewUIModel>(), DmxViewItemSourceChanged
                ));
            DmxViewSelectIndexProperty = DependencyProperty.Register("DmxViewSelectIndex", typeof(int), typeof(DMXViewPanelControl),
                new PropertyMetadata(0, DmxViewSelectIndexChanged
                ));
        }

        private static void DmxViewSelectIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DMXViewPanelControl ctr = d as DMXViewPanelControl;
            if (ctr == null)
            {
                return;
            }
            ctr.tc.SelectedIndex = (int)(e.NewValue);
          
         }
        private static void DmxViewItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DMXViewPanelControl ctr = d as DMXViewPanelControl;
            if (ctr == null)
            {
                return;
            }
            ctr.Dispatcher.Invoke(()=> {
                var source = e.NewValue as List<DMXViewUIModel>;

                ctr.tc.Items.Clear();
                for (int i = 0; i < source.Count; i++)
                {
                    var item = source[i];
                    var ui = new UI.DMXViewUIControl();
                    ui.SetBinding(DMXViewUIControl.DmxViewItemSourceProperty, new Binding("ItemSource") { Source = item, Mode = BindingMode.OneWay });
                    ui.DataContext = item;

                    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
                    sp.Children.Add(new TextBlock() { Text = item.Title, Margin = new Thickness(10, 3, 5, 3), FontSize = 14 });
                    Ellipse ellipse = new Ellipse() { Margin = new Thickness(5, 3, 10, 3), Width = 14, Height = 14, Fill = Brushes.Red };
                    ellipse.SetBinding(Ellipse.FillProperty, new Binding("StateColor") { Source = item, Mode = BindingMode.OneWay });
                    sp.Children.Add(ellipse);

                    TabItem ti = new TabItem();
                    ti.Header = sp;
                    ti.Content = ui;
                    ctr.tc.Items.Add(ti);
                }

            });
           
          
        }
        public List<DMXViewUIModel> DmxViewPanelSource
        {
            set => SetValue(DmxViewPanelSourceProperty, value);
            get => (List<DMXViewUIModel>)GetValue(DmxViewPanelSourceProperty);
        }
        public int DmxViewSelectIndex
        {
            set => SetValue(DmxViewSelectIndexProperty, value);
            get => (int)GetValue(DmxViewSelectIndexProperty);
        }
        public DMXViewPanelControl()
        {
            InitializeComponent();
        }
    }
}
